
//  ChooseView.m
//  ClassRoom
//
//  Created by apple on 12-12-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChooseView.h"
#import "Notifications.h"
#import <QuartzCore/QuartzCore.h>
//#import QuartzCore.framework

#define KIND_TAG 1001
#define PRICE_TAG 1002
#define ADDRESS_TAG 1003

@implementation ChooseView

@synthesize chooseArray;
@synthesize button;
@synthesize delegate;
@synthesize priceArray,addressArray,kindArrary;
@synthesize kindButton,addressButton,priceButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //self.backgroundColor = [UIColor redColor];
        frame = CGRectMake(0, 0, 108, 120);
        tableview = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.layer.masksToBounds = YES;
        tableview.layer.cornerRadius = 10;
        [self addSubview:tableview];
        
        float width = 320 / 3;
        kindButton = [UIButton buttonWithType:UIButtonTypeCustom];
        kindButton.frame = CGRectMake(0, 0, width, 30);
        [kindButton setImage:[UIImage imageNamed:@"全部类型.png"] forState:UIControlStateNormal];
        [kindButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        kindButton.tag = KIND_TAG;
        [self addSubview:kindButton];
        
        addressButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addressButton.frame = CGRectMake(width, 0, width, 30);
        [addressButton setImage:[UIImage imageNamed:@"全部类型.png"] forState:UIControlStateNormal];
        [addressButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        addressButton.tag = ADDRESS_TAG;
        [self addSubview:addressButton];
        
        priceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        priceButton.frame = CGRectMake(width*2, 0, width, 30);
        [priceButton setImage:[UIImage imageNamed:@"全部类型.png"] forState:UIControlStateNormal];
        [priceButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        priceButton.tag = PRICE_TAG;
        [self addSubview:priceButton];
        
        // array alloc
        chooseArray = [[NSMutableArray alloc] init];
        priceArray = [[NSMutableArray alloc] init];
        addressArray = [[NSMutableArray alloc] init];
        kindArrary = [[NSMutableArray alloc] init];
        
        tableview.hidden = YES;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)clickButton:(id) sender
{
    tableview.hidden = !tableview.hidden;
    
    UIButton *btn = (UIButton *)sender;
    if (btn.tag == ADDRESS_TAG) {
        viewShowType = addressType;
        tableview.frame = CGRectMake(320/3, 30, 320/3, 120);
    }
    else if(btn.tag == PRICE_TAG){
        viewShowType = priceType;
        tableview.frame = CGRectMake(320/3 * 2, 30, 320/3, 120);
    }
    else{
        viewShowType = kindType;
        tableview.frame = CGRectMake(0, 30, 320/3, 120);
    }
    
    [tableview reloadData];
}

- (void) setButtonEnable:(BOOL) enable
{
//    priceButton.enabled = enable;
//    addressButton.enabled = enable;
//    kindButton.enabled = enable;
}

- (void)reloadTableViewData{
    [tableview reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (viewShowType == priceType) {
        return [priceArray count];
    }
    else if( viewShowType == addressType ){
        return [addressArray count];
    }
    else{
        return [kindArrary count];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    cell.textLabel.textColor = [UIColor greenColor];
    NSString *title = nil;
    if (viewShowType == priceType) {
        title = [priceArray objectAtIndex:indexPath.row];
    }
    else if( viewShowType == addressType ){
         title = [addressArray objectAtIndex:indexPath.row];
    }
    else{
         title = [kindArrary objectAtIndex:indexPath.row];
    }
    cell.textLabel.text = title;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    tableview.hidden = !tableview.hidden;
    if ([delegate respondsToSelector:@selector(didSelecteIndex:title:)]) {
        NSString *title = nil;
        if (viewShowType == priceType) {
            title = [priceArray objectAtIndex:indexPath.row];
        }
        else if( viewShowType == addressType ){
            title = [addressArray objectAtIndex:indexPath.row];
        }
        else{
            title = [kindArrary objectAtIndex:indexPath.row];
        }
        [delegate didSelecteIndex:indexPath title:title];
    }
    
}

@end
